/**
*      author: Y_Yao
*      created: 2025-04-15 13:39:06
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 50 + 5;
int f[N][N][65];
int dis[N][N];
int n,m;

void floyd1(int w)
{
    for(int k = 1;k <= n;k++)
    {
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                if(f[i][k][w-1] && f[k][j][w-1])
                {
                    f[i][j][w] = 1;
                    dis[i][j] = 1;
                }
            }
        }
    }
}

void floyd2()
{
    for(int k = 1;k <= n;k++)
    {
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                dis[i][j] = min(dis[i][j],dis[i][k] + dis[k][j]);
            }
        }
    }
}

void solve()
{
    cin >> n >> m;
    memset(dis,INF,sizeof dis);
    for(int i = 1;i <= m;i++)
    {
        int u,v;
        cin >> u >> v;
        dis[u][v] = 1;
        f[u][v][0] = 1;
    }

    for(int i = 1;i <= 63;i++)
    {
        floyd1(i);
    }

    floyd2();

    cout << dis[1][n] << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}